package com.itheima.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.QuestionItem;
import com.itheima.service.store.QuestionItemService;
import com.itheima.service.store.impl.QuestionItemServiceImpl;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/store/questionItem")
public class QuestionItemServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        if("list".equals(operation)){
            //查询所有
            this.findAll(request,response);
        } else if("save".equals(operation)){
            //添加信息
            this.save(request,response);
        }else if("toEdit".equals(operation)){
            //跳转编辑页面
            this.toEdit(request,response);
        }else if("edit".equals(operation)){
            //保存修改信息
            this.edit(request,response);
        }else if("delete".equals(operation)){
            //删除信息
            this.delete(request,response);
        }/*else if("saveOrUpdate".equals(operation)){
            //删除信息
            this.delete(request,response);
        }*/
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //QuestionItemService questionItemService =new QuestionItemServiceImpl();
        QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class);
        questionItemService.delete(questionItem);
        findAll(request,response);
    }


    //跳转编辑页面
    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id =request.getParameter("id");
        QuestionItemService questionItemService =new QuestionItemServiceImpl();
        QuestionItem questionItem = questionItemService.findById(id);
        request.setAttribute("questionItem",questionItem);
        request.setAttribute("operation","edit");
        findAll(request,response);
    }


    //添加信息
    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //QuestionItemService questionItemService =new QuestionItemServiceImpl();
        QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class,"yyyy-MM-dd");
        questionItemService.save(questionItem);
        findAll(request,response);
    }

    //保存修改信息
    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        //QuestionItemService questionItemService =new QuestionItemServiceImpl();
        QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class,"yyyy-MM-dd");
        questionItemService.update(questionItem);
        findAll(request,response);
    }

   /* private void saveOrUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //QuestionItemService questionItemService =new QuestionItemServiceImpl();
        QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class,"yyyy-MM-dd");
        if(StringUtils.isNotBlank(questionItem.getId())){
            questionItemService.save(questionItem);
        }else {
            questionItemService.update(questionItem);
        }


        findAll(request,response);
    }*/



    //查询所有
    private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String questionId = request.getParameter("questionId");
        request.setAttribute("questionId",questionId);
        PageInfo all =questionItemService.findAll(questionId,1,100);
        request.setAttribute("page",all);
        if(request.getAttribute("operation") == null){
            request.setAttribute("operation","save");
        }
        request.getRequestDispatcher("/WEB-INF/pages/store/questionItem/list.jsp").forward(request,response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
